package com.qiantu.phone.ui.fragment.device;

import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qiantu.api.entity.ControlDataFreshAirBean;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class UniversalDeviceFragment extends BaseDeviceFragment{
    private WebView deviceWebview;
    private String url;

    Boolean isNew;

    public static UniversalDeviceFragment newInstance() {
        UniversalDeviceFragment fragment = new UniversalDeviceFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_universal;
    }

    @Override
    protected void initView() {
        super.initView();
        isNew = true;
        deviceWebview = findViewById(R.id.universal_webview);
        deviceWebview.getBackground().setAlpha(0);
        String deviceControl = "";
        if(mDevice.getDeviceType().equals("universal_freshair")){
            deviceControl = "freshairControl";
        }else if(mDevice.getDeviceType().equals("universal_sequencer")){
            deviceControl = "sequenceControl";
        }else if(mDevice.getDeviceType().equals("universal_projector")){
            deviceControl = "projectorControl";
        }else if(mDevice.getDeviceType().equals("universal_player")){
            deviceControl = "playControl";
        }else if(mDevice.getDeviceType().equals("universal_pa")){
            deviceControl = "powerAmplifierControl";
        }
        WebSettings webSettings = deviceWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        url = BuildConfig.DEVICE_URL + deviceControl + "/?token="+ AppApplication.getInstance().getLoginInfo().getToken() +"&houseId="+ AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo() + "&deviceSerialNo=" + mDevice.getDeviceSerialNo() + "&phone="+ AppApplication.getInstance().getUserPhone();
//        String url = "www.baidu.com";
        deviceWebview.loadUrl(url);
        //处理各种通知请求和事件，如果不使用该句代码，将使用内置浏览器访问网页
        deviceWebview.setWebViewClient(new WebViewClient());
        updateView();
    }

    protected void updateView() {
        super.updateView();
    }

    @Override
    public void onResume(){
        super.onResume();
        if(deviceWebview != null){
            deviceWebview.evaluateJavascript("refreshConfigUI", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {

                }
            });
        }
    }

    @Override
    public void onDestroy(){
        deviceWebview.removeAllViews();
        deviceWebview.destroy();
        deviceWebview = null;
        super.onDestroy();
    }
}
